#!/bin/bash
# This shell script used to install system tools

# this project absolute path
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

# top project absolute path
TOP_PATH=$(realpath $PRJ_PATH/..)

# shell script will exit once get command error
set -e

#+-------------------------+
#| Shell script functions  |
#+-------------------------+

function pr_error() {
    echo -e "\033[40;31m $1 \033[0m"
}

function pr_warn() {
    echo -e "\033[40;33m $1 \033[0m"
}

function pr_info() {
    echo -e "\033[40;32m $1 \033[0m"
}

function install_systools()
{
    if command -v jq > /dev/null 2>&1 ; then
        pr_info "All system tools already installed, skip it"
        return 0;
    fi

    pr_info "start apt install system tools(commands)"

    systools="coreutils jq wget curl tree gawk sed unzip cpio bc lzop zstd rsync kmod kpartx \
        desktop-file-utils iputils-ping xterm diffstat chrpath asciidoc docbook-utils help2man \
        build-essential gcc g++ make cmake automake groff socat flex texinfo bison texi2html \
        git cvs subversion mercurial autoconf autoconf-archive parted dosfstools lz4 liblz4-tool \
        python3 python3-pip python3-pexpect python3-git python3-jinja2 expect patchelf \
        lib32z1 libssl-dev libncurses5 libncurses-dev libgl1-mesa-dev libglu1-mesa-dev libsdl1.2-dev "

    apt update > /dev/null 2>&1
    apt install -y $systools
}


function install_devtools()
{
    if command -v debootstrap > /dev/null 2>&1 ; then
        pr_info "All development tools already installed, skip it"
        return 0;
    fi

    pr_info "start apt install devlopment tools(commands)"

    devtools="u-boot-tools mtd-utils device-tree-compiler binfmt-support fakeroot \
              live-build qemu qemu-user-static debootstrap debian-archive-keyring \
			  gcc-multilib g++-multilib "

    apt install -y $devtools
}


if [ `id -u` != 0 ] ; then
    echo ""
    pr_error "This shell script must be excuted as root privilege"
    exit;
fi

install_systools

install_devtools

